package com.shun.upms.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shun.common.R;
import com.shun.common.RC;
import com.shun.common.utils.ShunCommonUtils;
import com.shun.upms.common.UpmsConstant;
import com.shun.upms.dto.SysAdminRoleDto;
import com.shun.upms.dto.SysRoleMenuDto;
import com.shun.upms.mapper.SysAdminRoleMapper;
import com.shun.upms.service.ISysAdminRoleService;
import com.shun.upms.vo.SysAdminRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统管理角色表 服务实现类
 * </p>
 *
 * @author clever
 * @since 2021-09-27
 */
@Slf4j
@Service
public class SysAdminRoleServiceImpl extends ServiceImpl<SysAdminRoleMapper, SysAdminRole> implements ISysAdminRoleService {

    @Autowired
    private SysAdminRoleMapper roleMapper;

    @Override
    public List<String> getRolesByUser(String userId) {
        return roleMapper.getRolesByUserId(userId);
    }

    @Override
    public R addAminRole(SysAdminRoleDto dto) {
        log.debug("SysAdminRoleDto:{}", dto);
        SysAdminRole adminRole = ShunCommonUtils.beanMap(dto, SysAdminRole.class);
        adminRole.setRoleId(IdUtil.objectId());
        adminRole.setUpdateUser(StpUtil.getLoginIdAsString());
        adminRole.setCreateUser(StpUtil.getLoginIdAsString());
        log.debug("SysAdminRole:{}", adminRole);
        return save(adminRole) ? R.suc() : RC.DB_ADD_ERROR.toR();
    }

    @Override
    public R pageAdminRole(SysAdminRoleDto dto) {
        log.debug("SysAdminRoleDto:{},",dto.getLimit());
        Page<SysAdminRole> rolePage = dto.toPage();
        IPage<SysAdminRole> roleList = page(rolePage, new QueryWrapper<SysAdminRole>());
        return R.suc(roleList);
    }

    @Override
    public R getRoleNameAndId() {
        List<Map<String,Object>> roleNameAndIds = list().stream().map(r -> {
            Map<String,Object> roleMap = new HashMap<>();
            roleMap.put("name",r.getRoleName());
            roleMap.put("id",r.getRoleId());
            return roleMap;
        }).collect(Collectors.toList());
        return R.suc(roleNameAndIds);
    }

    @Override
    public R editAdminRole(SysAdminRoleDto dto) {
        UpdateWrapper<SysAdminRole> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("role_id",dto.getRoleId());
        SysAdminRole sysAdminRole = ShunCommonUtils.beanMap(dto,SysAdminRole.class);
        boolean flag = update(sysAdminRole,updateWrapper);
        return flag?R.suc():R.error(UpmsConstant.EDIT_ADMIN_DEPT_ERROR);
    }

    @Override
    public  R delRole(String roleId){
        int userCount = roleMapper.getUserByRoleId(roleId);
        if(userCount > 0){
            return R.error(UpmsConstant.DEL_ADMIN_ROLE_USERS_ERROR);
        }else{
           boolean flag = removeById(roleId);
           if(flag){
              roleMapper.delMenuByRoleId(roleId);
               return R.suc();
           }
            return R.error(UpmsConstant.DEL_ADMIN_ROLE_ERROR);
        }
    }

    @Override
    public R editRoleMenu(SysRoleMenuDto dto) {
        roleMapper.delMenuByRoleId(dto.getRoleId());
        dto.getMenuIds().forEach( s -> {roleMapper.editMenuRole(dto.getRoleId(),s);});
        return R.suc();
    }

    @Override
    public R editRoleUser(String roleId, String[] split) {
        roleMapper.delRoleAndUserIdByRoleId(roleId);
        Arrays.stream(split).forEach(s -> {roleMapper.addRoleAndUserId(s,roleId);});
        return R.suc();
    }
}